
import userModel from "../../models/user";
import Jwt from "../../prototype/jwt";
class User_management extends Jwt{
  constructor() {
    super();
    this.get_list = this.get_list.bind(this);
    this.do_delete = this.do_delete.bind(this);
    this.do_edit = this.do_edit.bind(this);
  }

  /**
   * 获取用户列表
   * pageNo: 1,  pageSize: 10,  username: '',
   * @param req
   * @param res
   * @returns {Promise<void>}
   */
  async get_list(req, res) {
    const body = {
      status: 200,
      msg: '请求成功'
    }

    const { pageNo, pageSize, username} = req.body;

    try {
      if([pageNo, pageSize].includes(undefined)) {
        throw ('参数错误')
      }
      const query = username ? {user_tel: username} : {}
      const users = await userModel.find(query).skip((req.body.pageNo - 1) * req.body.pageSize).limit(req.body.pageSize).sort({user_create: -1});
      body.totalCount = userModel.length;
      body.data = users || [];
    } catch (e) {
      body.status = 201
      body.msg = e.toString();
    }

    res.send(body)
  }

  do_edit(req, res) {
    const body = {
      status: 200,
      msg: '请求成功'
    }

    res.send(body)
  }

  do_delete(req, res) {
    const body = {
      status: 200,
      msg: '请求成功'
    }

    res.send(body)
  }
}

export default User_management;
